package io.github.source;

import android.content.Context;

import com.google.gson.Gson;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.DiskLogAdapter;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

import java.lang.ref.WeakReference;

/**
 * @ClassName: App
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: huangrongcai
 * @date: 2024/6/4
 * @Copyright: 黄嵘才（HuangRc)
 */
public final class Init {

    private WeakReference<Context> context;
    private static final Gson gson = new Gson();

    static {
        initLogAdapter();
    }

    public static Gson json() {
        //优化静态变量
        return gson;
    }

    private static class Loader {
        static volatile Init INSTANCE = new Init();
    }

    private static Init get() {
        return Loader.INSTANCE;
    }

    public static void init(Context context) {
        get().context = new WeakReference<>(context);

    }

    public static Context context() {
        return getContext();
    }

    public static Context getContext() {
        return get().context.get();
    }

    private static void initLogAdapter() {
        Logger.addLogAdapter(new AndroidLogAdapter(
                PrettyFormatStrategy.newBuilder()
                        .methodCount(2)
                        .showThreadInfo(true)
                        .tag("TV")
                        .build()) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return true;
            }
        });
        //输出日志到文件
        Logger.addLogAdapter(new DiskLogAdapter());
    }
}
